package com.zqxx.examine.management.controller.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.management.security.UserInfo;
import com.zqxx.examine.management.service.ExamPaperService;
import com.zqxx.examine.management.service.ExamService;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.exam.ExamHistory;
import com.zqxx.examine.model.exam.ExamPaper;
import com.zqxx.examine.model.question.PointStatistic;
import com.zqxx.examine.model.question.Question;
import com.zqxx.examine.model.question.QuestionFilter;
import com.zqxx.examine.model.user.Role;
import com.zqxx.examine.model.user.User;

@Controller
public class DashBoardAction
{
  @Autowired
  private QuestionService questionService;
  @Autowired
  private UserService userService;
  @Autowired
  private ExamPaperService examPaperService;
  @Autowired
  private ExamService examService;
  
  @RequestMapping(value={"/secure/dashboard/baseinfo"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public List<Integer> baseInfo(Model model)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Page<User> pageUser = new Page();
    pageUser.setPageNo(1);
    pageUser.setPageSize(1);
    this.userService.getUserListByRoleId(((Role)userInfo.getRoleMap().get("ROLE_STUDENT")).getRoleId(), pageUser);
    
    Page<Question> pageQuestion = new Page();
    pageQuestion.setPageNo(1);
    pageQuestion.setPageSize(1);
    QuestionFilter qf = new QuestionFilter();
    qf.setFieldId(0);
    qf.setKnowledge(0);
    qf.setQuestionType(0);
    qf.setTag(0);
    qf.setSearchParam("-1");
    this.questionService.getQuestionList(pageQuestion, qf);
    
    Page<ExamPaper> pageExamPaper = new Page();
    pageExamPaper.setPageNo(1);
    pageExamPaper.setPageSize(1);
    this.examPaperService.getEnabledExamPaperList(userInfo.getUsername(), pageExamPaper);
    List<Integer> l = new ArrayList();
    l.add(Integer.valueOf(pageQuestion.getTotalRecord()));
    l.add(Integer.valueOf(pageExamPaper.getTotalRecord()));
    l.add(Integer.valueOf(pageUser.getTotalRecord()));
    return l;
  }
  
  @RequestMapping(value={"/secure/dashboard/studentApprovedList"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public List<ExamHistory> studentApprovedList(Model model)
  {
    Page<ExamHistory> page = new Page();
    page.setPageNo(1);
    page.setPageSize(4);
    List<ExamHistory> histList = this.examService.getUserExamHistList(page, new int[] { 0 });
    
    return histList;
  }
  
  @RequestMapping(value={"/secure/dashboard/StudentMarkList"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public List<ExamHistory> studentMarkList(Model model)
  {
    Page<ExamHistory> page = new Page();
    page.setPageNo(1);
    page.setPageSize(4);
    List<ExamHistory> histList = this.examService.getUserExamHistList(page, new int[] { 2 });
    
    return histList;
  }
  
  @RequestMapping(value={"/secure/dashboard/chartinfo/{fieldId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public List<FieldNumber> chartInfo(Model model, @PathVariable("fieldId") int fieldId)
  {
    List<PointStatistic> pointStatisticList = this.questionService.getPointCount(fieldId, null);
    List<FieldNumber> l = new ArrayList();
    for (PointStatistic ps : pointStatisticList)
    {
      FieldNumber fieldNumber = new FieldNumber();
      fieldNumber.name = ps.getPointName();
      fieldNumber.amount = ps.getAmount();
      l.add(fieldNumber);
    }
    return l;
  }
  
  class FieldNumber
  {
    private String name;
    private int amount;
    
    FieldNumber() {}
    
    public String getName()
    {
      return this.name;
    }
    
    public void setName(String name)
    {
      this.name = name;
    }
    
    public int getAmount()
    {
      return this.amount;
    }
    
    public void setAmount(int amount)
    {
      this.amount = amount;
    }
  }
}
